
/* @flow */

import type Watcher from "./watcher";
import { remove } from "../util/index";
import config from "../config";

let uid = 0;

// 依赖收集
export default class Dep {

    static target: ?Watcher;
    id: Number;
    subs: Array<Watcher>;

    constructor () {
       this.id = uid++;
       this.subs = [];
    }

    addSub (sub: Watcher) {
        this.subs.push(sub);
    }

    removeSub (sub: Watcher) {
        remove(this.subs, sub);
    }

    depend () {
        if (Dep.target) {
            Dep.target.addDep(this)
        }
    }

    notify () {
        const subs = this.subs.slice();
        if (process.env.NODE_ENV !== "production" && !config.async) {
            subs.sort((a, b) => a.id - b.id);
        }
        for (let i = 0; i < subs.length; i++) {
            subs[i].update();
        }
    }
}

Dep.target = null;
const targetStack = [];

export function pushTarget(target: ?Watcher) {
    targetStack.push(target);
    Dep.target = target;
}

export function popTarget() {
    targetStack.pop();
    Dep.target = targetStack[targetStack.length - 1];
}